package org.springextensions.actionscript.mvcs.service.operation
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.springextensions.actionscript.mvcs.command.AbstractLoadCompleteCommand;

	public class URLLoaderOperation extends FileLoadOperation
	{
		private var _urlLoader:URLLoader;
		
		override protected function get bytesLoaded():int 
		{
			return (_urlLoader) ? _urlLoader.bytesLoaded : 0;
		}
		
		override protected function get bytesTotal():int 
		{
			return (_urlLoader) ? _urlLoader.bytesTotal : 0;
		}
		
		public function URLLoaderOperation(loadCompleteAction:AbstractLoadCompleteCommand)
		{
			super(loadCompleteAction);
		}
		
		override public function execute():void
		{
			_urlLoader = new URLLoader();
			_urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleError);
			_urlLoader.addEventListener(ProgressEvent.PROGRESS, dispatchProgress);
			
			_urlLoader.load(new URLRequest(url));
		}
		
		override public function get result():*
		{
			return _urlLoader.data;
		}
		
	}
}